package com.weasing.res.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.weasing.res.dao.IWxEvalresultDao;
import com.weasing.res.dao.IWxUserdetailDao;
import com.weasing.res.dao.IWxUserlistDao;
import com.weasing.res.domain.WxEvalresult;
import com.weasing.res.domain.WxUserdetail;
import com.weasing.res.domain.WxUserlist;
import com.weasing.res.service.IWxUserdetailService;
import com.weasing.res.utils.PageBean;
import com.weasing.res.vo.WxUserdetailVO;

@Service
@Transactional
public class WxUserdetailServiceImpl
		implements IWxUserdetailService {
	@Autowired
	private IWxUserdetailDao wxUserdetailDao;

	@Autowired
	private IWxUserlistDao wxUserlistDao;

	@Autowired
	private IWxEvalresultDao wxEvalresultDao;
	@Override
	public List<WxUserdetailVO> findWxUserdetailByCondition(int chargetype,
			String chargename, String totalscore) {
		List<Object[]> list = wxUserdetailDao.findWxUserdetailByCondition(
				chargetype, chargename, totalscore);
		// 将List数值转化为List WxUserdetailVO
		List<WxUserdetailVO> wxUserdetailVOList = new ArrayList<WxUserdetailVO>();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				WxUserdetailVO wxUserdetailVO = new WxUserdetailVO();
				WxUserdetail wxUserdetail = new WxUserdetail();
				WxUserlist wxUserlist = new WxUserlist();
				WxEvalresult wxEvalresult = new WxEvalresult();
				wxUserdetail.setId(Integer.parseInt(list.get(i)[4].toString()));//案件Id
				wxUserdetail.setChargetype(Integer.parseInt(list.get(i)[0].toString()));// 罪名
				wxUserdetail.setChargename(list.get(i)[1].toString());// 案件名称
				wxUserlist.setId(Integer.parseInt(list.get(i)[5].toString()));//嫌疑人Id
				wxUserlist.setTruename(list.get(i)[2].toString());// 姓名
				wxEvalresult.setId(Integer.parseInt(list.get(i)[6].toString()));//评估结果Id
				wxEvalresult.setTotalscore(Integer.parseInt(list.get(i)[3].toString()));// 总分值
				wxUserdetailVO.setWxUserdetail(wxUserdetail);
				wxUserdetailVO.setWxUserlist(wxUserlist);
				wxUserdetailVO.setWxEvalresult(wxEvalresult);
				wxUserdetailVOList.add(wxUserdetailVO);
			}
		}
		return wxUserdetailVOList;
	}

	@Override
	public int addWxUserdetail(WxUserdetail wxUserdetail) {
		wxUserdetailDao.save(wxUserdetail);
		return 0;
	}

	@Override
	public List<WxUserdetailVO> findAll() {
		List<Object[]> list = wxUserdetailDao.findWxUserdetailByCondition(0,null, "0");
		// 将List数值转化为List WxUserdetailVO
		List<WxUserdetailVO> wxUserdetailVOList = new ArrayList<WxUserdetailVO>();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				WxUserdetailVO wxUserdetailVO = new WxUserdetailVO();
				WxUserdetail wxUserdetail = new WxUserdetail();
				WxUserlist wxUserlist = new WxUserlist();
				WxEvalresult wxEvalresult = new WxEvalresult();
				int a = Integer.parseInt(list.get(i)[0].toString());
				wxUserdetail.setId(Integer.parseInt(list.get(i)[4].toString()));//案件Id
				wxUserdetail.setChargetype(Integer.parseInt(list.get(i)[0].toString()));// 罪名
				wxUserdetail.setChargename(list.get(i)[1].toString());// 案件名称
				wxUserlist.setId(Integer.parseInt(list.get(i)[5].toString()));//嫌疑人Id
				wxUserlist.setTruename(list.get(i)[2].toString());// 姓名
				wxEvalresult.setId(Integer.parseInt(list.get(i)[6].toString()));//评估结果Id
				wxEvalresult.setTotalscore(Integer.parseInt(list.get(i)[3].toString()));// 总分值
				wxUserdetailVO.setWxUserdetail(wxUserdetail);
				wxUserdetailVO.setWxUserlist(wxUserlist);
				wxUserdetailVO.setWxEvalresult(wxEvalresult);
				wxUserdetailVOList.add(wxUserdetailVO);
			}
		}
		return wxUserdetailVOList;
	}

	/**
	 * 查找所有数据个数
	 * 
	 * @return
	 */
	public Integer findCount() {
		Integer findCount = wxUserdetailDao.findCount();
		return findCount;
	}

	/**
	 * 根据id查询案件信息
	 * @param id
	 */
        @Override
	public WxUserdetail findById(int id) {
		return wxUserdetailDao.findById(id);
	}

	@Override
	public Boolean findUserdetailByUserId(Integer userid) {
		List<WxUserdetail> findUserdetailByUserId = wxUserdetailDao.findUserdetailByUserId(userid);
		if (null == findUserdetailByUserId) {
			return true;
		}
		return false;
	}

	/***
	 * 
	 * 
	 * 分页查询 wxUserdetailService
	 * 
	 * @author 黄龙
	 * 
	 */
	public void newFindAll(PageBean pageBean, Map<String, Object> map) {

		wxUserdetailDao.pageQuery(pageBean, map);
		List<WxUserdetail> rows = pageBean.getRows();
		for (WxUserdetail wxUserdetail : rows) {
			// 封装嫌疑人
			wxUserdetail.setWxUserlist_fordetail(
					wxUserlistDao.findById(wxUserdetail.getUserid()));
			// 封装评估结果
			wxUserdetail.setWxEvalresult_fordetail(
					wxEvalresultDao.findByUdetailid(wxUserdetail.getId()));
		}
	}

}
